package uia.alumni.profile;

import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.http.HttpServletRequest;
import uia.alumni.data.Role;
import uia.alumni.data.User;
import uia.alumni.web.Page;



/**
 * Make a form for editing the logged in user's profile.
 * @author Even Åby Larsen (even.larsen@uia.no)
 */
public class EditProfile extends Page implements Constants {

    /** SERVLET_NAME is used for consistency checking of web.xml */
    public static final String SERVLET_NAME = "profile.edit";

    @PersistenceUnit(name=PERSISTENCE_UNIT)
    private EntityManagerFactory emf;


    public EditProfile() {
        super(Role.member);
    }


    /**
     * Write a form for editing the user's profile.
     * 
     * @param out
     * @param request
     */
    public void writeBody(PrintWriter out, HttpServletRequest request) {
        EntityManager em = emf.createEntityManager();
        User user = getLoggedInUser(em, request);

        out.println("<h1>Edit profile</h1>");
        printStartForm(out, SaveProfile.SERVLET_NAME);
        out.println("<table>");
        printTextField(out, "Full name", FULLNAME, user.getFullName(), true);
        printTextField(out, "Address", ADDRESS, user.getAddress(), true);
        printTextField(out, "Phone", PHONE, user.getPhone(), true);
        printTextField(out, "Email", EMAIL, user.getUserName(), true);
        out.println("<tr><td colspan=\"2\">(Your email address is also your username.)</td></tr>");
        printTextArea(out, "Write a short description of yourself", DESCRIPTION, user.getDescription(), true);
        out.println("<tr><td><input type=\"submit\" value=\"Save changes\"/></td></tr>");
        out.println("</table>");
        out.println("</form>");
    }


    public final static long serialVersionUID = 1;

}
